package com.leps.nq.tx.core;

import cn.hutool.core.convert.Convert;
import com.leps.nq.common.util.SpringUtils;
import com.leps.nq.sys.entity.SysConfig;
import com.leps.nq.sys.service.ISysConfigService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SysConfigs {
    private static List<SysConfig> sysConfigs;

    public static List<SysConfig> getSysConfigs() {
        if (sysConfigs == null) {
            loadSysConfigs();
        }
        return sysConfigs;
    }

    public static synchronized void loadSysConfigs() {
        if(sysConfigs == null) {
            ISysConfigService configService = SpringUtils.getBean(ISysConfigService.class);
            sysConfigs = configService.list();
        }
    }

    public static int getConfigValueAsInt(String configKey) {
        List<SysConfig> sysConfigs = getSysConfigs();
        Map<String, String> map = sysConfigs.stream().collect(Collectors.toMap(c -> c.getConfigKey(), c -> c.getConfigValue()));
        String configValue = map.get(configKey);
        return Convert.toInt(configValue);
    }

    public static int liquidLevelUpperLimit() {
        return getConfigValueAsInt("LIQUID_LEVEL_UPPER_LIMIT");
    }

    public static int liquidLevelLowerLimit() {
        return getConfigValueAsInt("LIQUID_LEVEL_LOWER_LIMIT");
    }

    public static int pessureUpperLimit() {
        return getConfigValueAsInt("PRESSURE_UPPER_LIMIT");
    }

    public static int pressureLowerLimit() {
        return getConfigValueAsInt("PRESSURE_LOWER_LIMIT");
    }

    public static int plcReconnectInterval() {
        return getConfigValueAsInt("PLC_RECONNECT_INTERVAL");
    }

    public static int plcReconnectTimeout() {
        return getConfigValueAsInt("PLC_RECONNECT_TIMEOUT");
    }

    public static int plcFetchInterval() {
        return getConfigValueAsInt("PLC_FETCH_INTERVAL");
    }
}
